/*
 * libjpeg  gsnap.tar.gz   /dev/graphics/fb0
 *参考http://blog.csdn.net/armeasy/article/details/6199879
 */

#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <stdlib.h>
#include "org_screenshot_ScreenShot.h"
#include "gsnap.h"

JNIEXPORT jstring JNICALL Java_org_screenshot_ScreenShot_shot__Ljava_lang_String_2(
		JNIEnv * env, jobject jobj, jstring jpath) {
	const char * default_path = "/mnt/sdcard/screenshot.jpeg";
	const char* fb_name = "/dev/graphics/fb0";
	char *path = NULL;
	if (NULL != jpath) {
		const char *path_tmp = (*env)->GetStringUTFChars(env, jpath, JNI_FALSE);
		path = (char *) malloc(strlen(path_tmp) + 1);
		strcpy(path, path_tmp);
		(*env)->ReleaseStringUTFChars(env, jpath, path_tmp);
	} else {
		path = (char *) malloc(strlen(default_path) + 1);
		strcpy(path, default_path);
	}
	LOGD("path size %d", strlen(path) + 1);
	LOGD("path %s", path);
	FBInfo fb;
	memset(&fb, 0x00, sizeof(fb));
	if (fb_open(&fb, fb_name) == 0) {
		if (strstr(path, ".png") != NULL) {
			//snap2png(filename, 100, &fb);
		} else {
			snap2jpg(path, 80, &fb);
		}
		fb_close(&fb);
	}
	LOGD("file %s created successfully\n", path);
	return (*env)->NewStringUTF(env, path);
}

